const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = (env) => {
    let plugins = []
    if (env.production) {
        plugins.push(new MiniCssExtractPlugin({
            filename: 'style/[name].[contenthash:8].css'
        }))
    }
    return {
        mode: env.production ? 'production' : 'development',
        entry: './src/index.ts',
        output: {
            filename: 'js/[name].[contenthash:8].js',
            path: path.resolve(__dirname, './dist'),
            clean: true,
            publicPath: '/',
        },
        resolve: {
            extensions: ['.ts', '...']
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: './src/index.html',
                inject: 'body',
            }),
            // css单独打包，不放入js
            ...plugins
        ],
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    use: 'ts-loader',
                },
                {
                    test: /\.(css|less)$/,
                    use: [
                        env.production ? MiniCssExtractPlugin.loader : 'style-loader',
                        {
                            loader: 'css-loader',
                            options: {
                                importLoaders: 2, // 'postcss-loader','less-loader',
                            },
                        },
                        'postcss-loader',
                        {
                            loader: 'less-loader',
                            options: {
                                // lessOptions: {
                                //     strictMath: true,
                                // }
                            }
                        }
                    ]
                }
            ]
        },
        devServer: {
            // static: path.resolve(__dirname, './dist'),
            port: 9000,
            open: true,
            hot: true,
        },
    }
}